# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
  def get_host_name
    host = request.domain
    lel = Level.find_last_by_host host.swapcase
    le_default = Level.find_last_by_is_default true
    @hots_name = le_default.name
    unless lel.nil?
      @hots_name = lel.name
    end
  end

  def get_lastest_imgs
    host = request.domain
    le = $level_map[host.swapcase]   
    res_l = le[:res_level]

    resouces = Resource.find :all,
      :conditions =>  ["status=?  and level <= ? and created_at >=? ", 1, res_l,Time.now.strftime("%Y-%m-%d")],
      :order => 'created_at desc'      
    if resouces.size < 1
      ress = Resource.find :all,
        :conditions => ["status=? and level <= ?",1,res_l],
        :order => 'created_at desc',
        :limit => '1'
      if ress.size > 0
        resouces = Resource.find :all,
          :conditions =>  ["status=?  and level <= ? and created_at >=?", 1, res_l,ress[0].created_at],
          :order => 'created_at desc'
      end
    end
    @lastest_count = 0
    resouces.each do |res|
      @lastest_count = @lastest_count + get_pic_num(res)
    end
    @lastest_count = @lastest_count.to_i
  end
  #得到图片数量
  def get_pic_num resource
    src_arry = []
    small_path = File.join($upload_path,resource.res_dir,$small)
    src_arry = DataFile.traverse_dir(small_path, src_arry)
    @pic_size = src_arry.size
  end

  #更多标签
  def more_ser
    @img_style = '1'
    if params[:img_style].nil?
      @img_style = '1'
    else
      @img_style = params[:img_style]
    end
    @back_type = nil
    @pp_style = params[:pp_style]
    if @pp_style.nil?
      @pp_style = 1
    end
    @pp_num = params[:pp_num]
    if @pp_num.nil?
      @pp_num = 2
    end
    tags = "性感,美腿,美女"
    tag_array = tags.split(",")
    @retrun_array = []
    tag_array.each do |tag_name|
      tag = Tag.find_last_by_name tag_name
      unless tag.nil?
        @retrun_array << tag
      end
    end
  end

  def tims
    t = Time.now   
    @return_date = case t.to_a[6]
    when 1
      "星期一"
    when 2
      "星期二"
    when 3
      "星期三"
    when 4
      "星期四"
    when 5
      "星期五"
    when 6
      "星期六"
    else
      "星期日"
    end
    @return_date = @return_date + " " + Time.now.strftime("%m-%d %H:%M")
    #Time.now.strftime("%Y-%m-%d %H:%M:%S")
  end

end
